package com.kuiandjie.web;


import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kuiandjie.entity.Employee;
import com.kuiandjie.entity.Files;
import com.kuiandjie.service.FileService;

public class DelFileServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String fileid = request.getParameter("fileid");
		Employee emp = (Employee) session.getAttribute("employee");
		String fid = (String) session.getAttribute("folderid");
		if(fileid == null || "".equals(fileid)){
			response.sendRedirect("filelist.jspx?fid=" + fid);
		}else{
			FileService fs = new FileService();
			
			Files f = fs.findByIdAndEmpId(fileid,emp.getId());
			if(f == null){
				response.sendError(403,"你无权进行此操作！");
			}else{
				 String path = getServletContext().getRealPath("/");
				
				File file = new File(path +"/upload/" +f.getFileName());
				file.delete();
				fs.delById(fileid);
				
			}
		}
		
		
	}

}
